package com.yunhe.marketing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.marketing.constant.EnterpriseConsts;
import com.yunhe.marketing.domain.Enterprise;
import com.yunhe.marketing.esdomain.EsDataClueDemand;
import com.yunhe.marketing.esrepository.EsDataClueDemandrepository;
import com.yunhe.marketing.model.ClueQueryCondition;
import com.yunhe.marketing.model.DemandQueryCondition;
import com.yunhe.marketing.repository.EnterpriseRepository;
import com.yunhe.marketing.service.ITaskService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: DongWenYu
 * @Date: 2021/2/26 13:44
 */
@Service
public class TaskServiceImpl implements ITaskService {

    @Autowired
    EnterpriseRepository enterpriseRepository;

    @Autowired
    EsDataClueDemandrepository esDataClueDemandrepository;

    @Override
    public PageResult selectResponsibleClue(Long responsiblePersonId , int page , int size) {
        ClueQueryCondition clueQueryCondition = new ClueQueryCondition();
        clueQueryCondition.setResponsiblePerson(responsiblePersonId);
        PageHelper.startPage(page,size);
        List<Enterprise> enterpriseList = enterpriseRepository.selectClueByClueQueryConditionForTask(clueQueryCondition);
        PageInfo<Enterprise> pageInfo = new PageInfo<>(enterpriseList);
        return new PageResult(page,size, (int) pageInfo.getTotal(),enterpriseList);
    }

    @Override
    public PageResult selectResponsibleDemand(Long responsiblePersonId, int page, int size) {
        DemandQueryCondition demandQueryCondition = new DemandQueryCondition();
        demandQueryCondition.setResponsiblePerson(responsiblePersonId);
        PageHelper.startPage(page,size);
        List<Enterprise> enterpriseList = enterpriseRepository.selectDemandByDemandQueryConditionForTask(demandQueryCondition);
        PageInfo<Enterprise> pageInfo = new PageInfo<>(enterpriseList);
        return new PageResult(page,size, (int) pageInfo.getTotal(),enterpriseList);
    }

    @Override
    public Enterprise feedBack(Long id,Long responsibleId , String feedbackStatus, String conversationContent) {
        Enterprise enterprise = enterpriseRepository.selectEnterpriseById(id);
        if (enterprise != null) {
            if (responsibleId.equals(enterprise.getResponsiblePerson())) {
                enterprise.setConversationContent(conversationContent);
                enterprise.setFeedbackStatus(feedbackStatus);
                enterprise.setAllocationStatus(EnterpriseConsts.ALLOCATION_STATE.KnockedBack.value());
                enterpriseRepository.updateById(enterprise);

                EsDataClueDemand esDataClueDemand = new EsDataClueDemand();
                BeanUtils.copyProperties(enterprise, esDataClueDemand);
                esDataClueDemandrepository.save(esDataClueDemand);
            }
        }
        return enterprise;
    }
}
